<?php
ob_start();
//include('common/db.php');
include('common/xta.php');
include('common/xpath.php');

$xta = new xta();
$xp = new xp();

$stock_code = $_GET['code'];
$show_type = $_GET['show'];

$all_moves = array();

if(!empty($stock_code)) {
	
	if(!empty($show_type))
		$xta->load($stock_code);
	else
		$xta->load($stock_code);
		
	$all_moves = $xta->data['days'];	
}

//print_r($xta->data);
array_shift($all_moves);
//print_r($all_moves);


$sorted = array();
foreach($all_moves as $key => $each_move) {
	$date = explode("/", $each_move['day']);	
	$timestamp = mktime(0,0,0,$date['1'],$date['0'],$date['2']);
	$sorted[$timestamp] = $key;
}
krsort($sorted);
//print_r($sorted);


if(isset($_GET['ajax'])) {
	$data = array();
	
	foreach($all_moves as $each_move) {
		
		$date = explode("/", $each_move['day']);
		$timestamp = strtotime($date['0']." day ".$date['1']." month ".$date['2']." year");
		$data[$timestamp] = $each_move['wap'];	
	}
	ksort($data);
	if(count($data) > 0)
		echo implode(",", $data);
	else
		echo "0";
	exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
<link rel="stylesheet" type="text/css" href="jquery.tablesorter/themes/blue/style.css"></link>
<script type="text/javascript" src="jquery.tablesorter/jquery-latest.js"></script>
<script type="text/javascript" src="jquery.tablesorter/jquery.tablesorter.js"></script>
<script type="text/javascript">
	$(function() {
		$("#companies").tablesorter();
	});
</script>
<body>
<table id="companies" cellspacing="0" class="tablesorter">
	<thead>
		<tr>
			<th>Date</th>
			<th>Open</th>
			<th>Close</th>
			<th>Avg</th>			
		</tr>
	</thead>
	<tfoot>
		<tr>
			<th>Date</th>
			<th>Open</th>
			<th>Close</th>
			<th>Avg</th>
		</tr>
	</tfoot>
	<tbody>
	<?php foreach($sorted as $each_order) { $each_move = $all_moves[$each_order];?>
	<tr>
		<td><?=$each_move['day']?></td>
		<td><?=$each_move['open_price']?></td>
		<td><?=$each_move['close_price']?></td>
		<td><?=$each_move['wap']?></td>
	</tr>
	<?php } ?>
	</tbody>
</table>
</body>
</html>
